﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Device.Location;
using Microsoft.Devices.Sensors;
using System.Windows.Threading;

namespace MSRoomLocator.ViewModels
{
    public class LocationService
    {
        public GeoCoordinateWatcher Watcher;
        
        public string status;
        public GeoCoordinate MyLocation;
        public FloorPlan FloorPlan;

        //public Compass compass;
        //DispatcherTimer timer;
        //string accuracyText = "";
        //double magneticHeading;
        //double trueHeading;
        //double headingAccuracy;
        //bool isDataValid;
        //Accelerometer accelerometer;

        //bool calibrating = false;


        public LocationService()
        {
            //InitialLocationService();
        }

        void timer_Tick(object sender, EventArgs e)
        {
            throw new NotImplementedException();
        }

        #region Application logic
        
        //private void InitialLocationService()
        //{
        //    Watcher = new GeoCoordinateWatcher(GeoPositionAccuracy.High);
        //    Watcher.MovementThreshold = 2.5;

        //    // Add event handlers for StatusChanged and PositionChanged events
        //    Watcher.StatusChanged += new EventHandler<GeoPositionStatusChangedEventArgs>(watcher_StatusChanged);
        //    Watcher.PositionChanged += new EventHandler<GeoPositionChangedEventArgs<GeoCoordinate>>(watcher_PositionChanged);

        //}

        ///// <summary>
        ///// Helper method to start up the location data acquisition
        ///// </summary>
        ///// <param name="accuracy">The accuracy level </param>
        //public void StartLocationService()
        //{
        //    // Reinitialize the GeoCoordinateWatcher
        //    status = "starting, " + accuracyText;
        //    if (FloorPlan != null)
        //    {
        //        FloorPlan.UpdateStatus(status);
        //    }
            
        //    if (!Watcher.TryStart(false, TimeSpan.FromSeconds(5)))
        //    {
        //        status = "Operation timeout";
        //    }

        //    if (FloorPlan != null)
        //    {
        //        FloorPlan.UpdateStatus(status);
        //    }
        //}

        //public void StopLocationService()
        //{
        //    Watcher.Stop();
        //}

        ///// <summary>
        ///// Handler for the StatusChanged event. This invokes MyStatusChanged on the UI thread and
        ///// passes the GeoPositionStatusChangedEventArgs
        ///// </summary>
        ///// <param name="sender"></param>
        ///// <param name="e"></param>
        //void watcher_StatusChanged(object sender, GeoPositionStatusChangedEventArgs e)
        //{
        //    Deployment.Current.Dispatcher.BeginInvoke(() => MyStatusChanged(e));

        //}
        ///// <summary>
        ///// Custom method called from the StatusChanged event handler
        ///// </summary>
        ///// <param name="e"></param>
        //void MyStatusChanged(GeoPositionStatusChangedEventArgs e)
        //{
        //    switch (e.Status)
        //    {
        //        case GeoPositionStatus.Disabled:
        //            // The location service is disabled or unsupported.
        //            // Alert the user
        //            status = "Geo location service is unsupported on this device";
        //            break;
        //        case GeoPositionStatus.Initializing:
        //            // The location service is initializing.
        //            // Disable the Start Location button
        //            status = "Initializing location service," + accuracyText;
        //            break;
        //        case GeoPositionStatus.NoData:
        //            // The location service is working, but it cannot get location data
        //            // Alert the user and enable the Stop Location button
        //            status = "Data unavailable," + accuracyText;
        //            break;
        //        case GeoPositionStatus.Ready:
        //            // The location service is working and is receiving location data
        //            // Show the current position and enable the Stop Location button
        //            status = "Receiving data, " + accuracyText;
        //            break;

        //    }

        //    if (FloorPlan != null)
        //    {
        //        FloorPlan.UpdateStatus(status);
        //    }
        //}

        ///// <summary>
        ///// Handler for the PositionChanged event. This invokes MyStatusChanged on the UI thread and
        ///// passes the GeoPositionStatusChangedEventArgs
        ///// </summary>
        ///// <param name="sender"></param>
        ///// <param name="e"></param>
        //void watcher_PositionChanged(object sender, GeoPositionChangedEventArgs<GeoCoordinate> e)
        //{
        //    Deployment.Current.Dispatcher.BeginInvoke(() => MyPositionChanged(e));
        //}

        ///// <summary>
        ///// Custom method called from the PositionChanged event handler
        ///// </summary>
        ///// <param name="e"></param>
        //void MyPositionChanged(GeoPositionChangedEventArgs<GeoCoordinate> e)
        //{
        //    MyLocation = e.Position.Location;
        //    App.ViewModel.MyLocation = MyLocation;
        //    if (FloorPlan != null)
        //    {
        //        FloorPlan.UpdateLocation();
        //    }
            
        //}

        #endregion
    }
}
